package com.luck.easyfoodbatch.ease;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.util.ACache;
import com.luck.easyfoodbatch.util.Tools;

public class EaseUserUtils {

    static EaseUI.EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * get EaseUser according username
     *
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        if (userProvider != null)
            return userProvider.getUser(username);

        return null;
    }

    /**
     * set user avatar
     *
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView) {
        EaseUser user = getUserInfo(username);
        if (user != null && user.getAvatar() != null) {
            try {
                int avatarResId = Integer.parseInt(user.getAvatar());
                Glide.with(context).load(avatarResId).into(imageView);
            } catch (Exception e) {
                //use default avatar
                Glide.with(context).load(user.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.ease_default_avatar).into(imageView);
            }
        } else {
            Glide.with(context).load(R.mipmap.ease_default_avatar).into(imageView);
        }
    }

    /**
     * put user avatar
     *
     * @return
     */
    public static void putUserAvatar(Context context, String key, String avatar) {
        ACache aCache = ACache.get(context);
        aCache.put(key + "_avatar", avatar);
    }

    /**
     * set user avatar
     *
     * @return
     */
    public static void setUserAvatarPath(Context context, String username, ImageView imageView) {
        ACache aCache = ACache.get(context);
        String avatar = aCache.getAsString(username + "_avatar");
        if (!Tools.isNull(avatar)) {
            Glide.with(context).load(avatar).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.ease_default_avatar).into(imageView);
        } else {
            Glide.with(context).load(R.mipmap.ease_default_avatar).into(imageView);
        }
    }

    /**
     * put user nickname
     *
     * @return
     */
    public static void putUserNickname(Context context, String key, String nickname) {
        ACache aCache = ACache.get(context);
        aCache.put(key + "_nick", nickname);
    }

    /**
     * set user nickname
     *
     * @return
     */
    public static void setUserNickname(Context context, String username, TextView textView) {
        ACache aCache = ACache.get(context);
        String nickname = aCache.getAsString(username + "_nick");
        if (textView != null) {
            if (!Tools.isNull(nickname)) {
                if (Tools.validatePhone(nickname)) {
                    // 是手机号
                    nickname = nickname.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    textView.setText(nickname);
                } else {
                    textView.setText(nickname);
                }
            } else {
                textView.setText(username);
            }
        }
    }

    /**
     * get user nickname
     *
     * @return
     */
    public static String getUserNickname(Context context, String username) {
        ACache aCache = ACache.get(context);
        String nickname = aCache.getAsString(username + "_nick");
        return nickname;
    }

    /**
     * set user's nickname
     */
    public static void setUserNick(String username, TextView textView) {
        if (textView != null) {
            EaseUser user = getUserInfo(username);
            if (user != null && user.getNick() != null) {
                textView.setText(user.getNick());
            } else {
                textView.setText(username);
            }
        }
    }


}
